package com.jeady.timetodo.activities.common

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.jeady.jxcompose.ClickEffect.longClickSaveToDCIM
import com.jeady.timetodo.R
import com.jeady.timetodo.activities.task.TaskType
import com.jeady.timetodo.base.TimeSlider
import com.jeady.timetodo.base.Title
import com.jeady.timetodo.services.updateRunningService
import com.jeady.timetodo.ui.theme.TimeToDoTheme

@Composable
fun CommonTaskUI(title: String, subTitle: String="", actions: List<TaskAction> = listOf(), enabled: Boolean=true,
                 beforeConfigure: @Composable ColumnScope.()->Unit
) {
    TaskCommonContainer(title, subTitle) {
        TaskConfigure(actions, enabled, beforeConfigure)
    }
}

@Composable
fun TaskCommonContainer(title: String, subTitle: String, content: @Composable () -> Unit) {
    val context = LocalContext.current
    TimeToDoTheme {
        Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
            Column(
                Modifier
                    .align(Alignment.TopCenter)
                    .padding(top = 50.dp)
            ) {
                Title(title, subTitle)
            }
            Surface(shadowElevation = 3.dp) {
                Column(
                    Modifier
                        .fillMaxWidth(0.9f)
                        .padding(10.dp)
                ) {
                    Image(
                        painter = painterResource(id = R.drawable.gzh),
                        contentDescription = "关注公众号",
                        Modifier.longClickSaveToDCIM(context, R.drawable.gzh)
                    )
                    content()
                }
            }
        }
    }
}

@Composable
private fun TaskConfigure(
    actions: List<TaskAction>,
    enabled: Boolean,
    configBefore: @Composable() (ColumnScope.() -> Unit)
){
    var timeSetting by remember { mutableLongStateOf(0) }
    Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
        configBefore()
        TimeSlider {
            timeSetting = it
        }
        TaskConfigureAction(timeSetting, actions, enabled)
    }
}

@Composable
private fun TaskConfigureAction(timeSetting: Long, actions: List<TaskAction>, enabled: Boolean){
    val context = LocalContext.current
    LazyColumn(
        Modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(20.dp)
    ) {
        items(actions){
            if(it.text.isNotBlank()) {
                OutlinedButton(onClick = {
                    updateRunningService(context, it.taskType, timeSetting)
                }, Modifier.fillMaxWidth(), enabled=enabled) {
                    Text("${timeSetting / 1000}秒后\n${it.text}", textAlign = TextAlign.Center)
                }
            }
        }
    }
}

data class TaskAction(val text:String, val taskType: TaskType, val options: List<String> = listOf())

private fun getTip(taskType: TaskType): String {
    return when (taskType) {
        TaskType.LIGHT_ON -> "亮灯"
        TaskType.LIGHT_OFF -> "关灯"
        TaskType.VIBRATE -> "震动"
        TaskType.STOP_VIBRATE -> "停止震动"
        TaskType.STOP_SELF -> "停止服务"
        TaskType.LOCK_SCREEN -> "锁屏"
        else->""
    }
}